package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.StockBo;
import com.ruoyi.supplier.domain.vo.StockVo;
import com.ruoyi.supplier.domain.Stock;
import com.ruoyi.supplier.mapper.StockMapper;
import com.ruoyi.supplier.service.IStockService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 仓库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class StockServiceImpl implements IStockService {

    private final StockMapper baseMapper;

    /**
     * 查询仓库
     */
    @Override
    public StockVo queryById(Integer stockId){
        return baseMapper.selectVoById(stockId);
    }

    /**
     * 查询仓库列表
     */
    @Override
    public TableDataInfo<StockVo> queryPageList(StockBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Stock> lqw = buildQueryWrapper(bo);
        Page<StockVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询仓库列表
     */
    @Override
    public List<StockVo> queryList(StockBo bo) {
        LambdaQueryWrapper<Stock> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Stock> buildQueryWrapper(StockBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Stock> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMerchantId() != null, Stock::getMerchantId, bo.getMerchantId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Stock::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增仓库
     */
    @Override
    public Boolean insertByBo(StockBo bo) {
        Stock add = BeanUtil.toBean(bo, Stock.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStockId(add.getStockId());
        }
        return flag;
    }

    /**
     * 修改仓库
     */
    @Override
    public Boolean updateByBo(StockBo bo) {
        Stock update = BeanUtil.toBean(bo, Stock.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Stock entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除仓库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
